package com.sangmo.fts.user.service;

import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.user.model.Permission;
import com.sangmo.fts.user.repo.PermissionRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class PermissionEntityService extends BaseViewService<Permission> {

    @Autowired
    private PermissionRepo permissionRepo;

    @Override
    protected IDataViewRepo<Permission> getRepo() {
        return permissionRepo;
    }

    public Collection<String> findByApplicationAndRoles(String application, Set<String> roles) {
        return permissionRepo.findByApplicationAndRoleIn(application, roles);
    }

    public Collection<String> findByApplication(String application) {
        Collection<Permission> perms = permissionRepo.findByApplication(application);
        List<String> uris = perms.stream().map(Permission::getUri).collect(Collectors.toList());
        return uris;
    }
}
